package com.dawn.module.cms.controller.app.article;

import com.dawn.framework.common.enums.CommonStatusEnum;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.cms.controller.admin.article.vo.ArticleSaveReqVO;
import com.dawn.module.cms.controller.app.article.vo.AppArticlePageReqVO;
import com.dawn.module.cms.controller.app.article.vo.AppArticleRespVO;
import com.dawn.module.cms.convert.article.ArticleConvert;
import com.dawn.module.cms.dal.dataobject.article.CmsArticleDO;
import com.dawn.module.cms.dal.dataobject.category.CategoryDO;
import com.dawn.module.cms.service.article.CmsArticleService;
import com.dawn.module.cms.service.category.CmsCategoryService;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import static com.dawn.framework.common.pojo.CommonResult.success;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Tag(name = "用户 APP - 文章")
@RestController
@RequestMapping("/cms/article")
@Validated
public class AppCmsArticleController {

    @Resource
    private CmsArticleService articleService;

    @Resource
    private CmsCategoryService categoryService;

    @GetMapping("/get")
    @Operation(summary = "获得文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppArticleRespVO> getArticle(@RequestParam("id") Long id) {
        CmsArticleDO article = articleService.getArticle(id);
        AppArticleRespVO articleRespVO = BeanUtils.toBean(article, AppArticleRespVO.class);
        if(articleRespVO.getPublish().equals(CommonStatusEnum.DISABLE.getStatus())
            || articleRespVO.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())){
            return null;
        }
        if(article!=null){
            if(article.getCategoryId()!=null){
                CategoryDO categoryDO = categoryService.getCategory(article.getCategoryId());
                articleRespVO.setCategoryName(categoryDO.getName());
                articleRespVO.setCategoryPId(categoryDO.getParentId());
            }
            if(article.getTags()!=null){
                String[] tagsArray = article.getTags().split(",");
                articleRespVO.setTagsArray(tagsArray);
            }
            ArticleSaveReqVO articleSaveReqVO = new ArticleSaveReqVO();
            articleSaveReqVO.setId(article.getId());
            int views = 1;
            if(article.getViews() != null){
                views += article.getViews();
            }
            articleSaveReqVO.setViews(views);
            articleSaveReqVO.setSort(article.getSort());
            articleSaveReqVO.setStatus(article.getStatus());
            articleService.updateArticle(articleSaveReqVO);
        }

        return success(articleRespVO);
    }

    @GetMapping("/add-browse-count")
    @Operation(summary = "点击文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult addBrowseCount(@RequestParam("id") Long id) {
        CmsArticleDO article = articleService.getArticle(id);
        if(article!=null){
            ArticleSaveReqVO articleSaveReqVO = new ArticleSaveReqVO();
            articleSaveReqVO.setId(article.getId());
            int views = 1;
            if(article.getViews() != null){
                views += article.getViews();
            }
            articleSaveReqVO.setViews(views);
            articleSaveReqVO.setSort(article.getSort());
            articleSaveReqVO.setStatus(article.getStatus());
            articleService.updateArticle(articleSaveReqVO);
        }
        return success(article);
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章分页")
    public CommonResult<PageResult<AppArticleRespVO>> getArticlePage(@Valid AppArticlePageReqVO pageReqVO) {
//        pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        PageResult<CmsArticleDO> pageResult = articleService.getArticlePage(pageReqVO);
        return success(new PageResult<>(ArticleConvert.INSTANCE.convertList0(pageResult.getList()),
                pageResult.getTotal()));
    }

    @RequestMapping("/list")
    @Operation(summary = "获得文章列表分页")
    public CommonResult<PageResult<AppArticleRespVO>> getArticleList(AppArticlePageReqVO pageReqVO) {
//        pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setTopping(CommonStatusEnum.ENABLE.getStatus());
        List<CmsArticleDO> cmsArticleList = new ArrayList<>();
        PageResult<CmsArticleDO> result = null;
        if(pageReqVO.getRecommendHot()!=null && pageReqVO.getRecommendHot()){
            result = articleService.getArticlePage(pageReqVO);
            cmsArticleList = result.getList();
        }else if (pageReqVO.getRecommendBanner()!=null && pageReqVO.getRecommendBanner()){
            result = articleService.getArticlePage(pageReqVO);
            cmsArticleList = result.getList();
            if(cmsArticleList!=null && cmsArticleList.size()>0){
                cmsArticleList = cmsArticleList.stream().filter(article -> (article.getCoverImg()!=null && !"".equals(article.getCoverImg()))).collect(Collectors.toList());
            }
        }
        return success(new PageResult<>(ArticleConvert.INSTANCE.convertList0(cmsArticleList),
                result.getTotal()));
    }
}